package io.renren.modules.blog.service.impl;

import io.renren.common.exception.ErrorCode;
import io.renren.common.exception.RenException;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.TreeUtils;
import io.renren.modules.blog.dao.BlogRouterDao;
import io.renren.modules.blog.dto.BlogRouterDto;
import io.renren.modules.blog.entity.BlogRouterEntity;
import io.renren.modules.blog.service.BlogRouterService;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.enums.SuperAdminEnum;
import io.renren.modules.sys.service.SysRoleRouterService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 博客路由表
 */
@Service
@AllArgsConstructor
public class BlogRouterServiceImpl extends BaseServiceImpl<BlogRouterDao, BlogRouterEntity> implements BlogRouterService {

    private final SysRoleRouterService sysRoleRouterService;
    @Override
    public List<BlogRouterDto> getBlogRouterList(UserDetail user, Integer menuType) {
        List<BlogRouterEntity> routerList;

        //系统管理员，拥有最高权限
        if (user.getSuperAdmin() == SuperAdminEnum.YES.value()) {
            routerList = baseDao.getAllRouterList(menuType);
        } else {
            routerList = baseDao.getBlogRouterList(user.getId(), menuType);
        }
        List<BlogRouterDto> dtoList = ConvertUtils.sourceToTarget(routerList, BlogRouterDto.class);

        return TreeUtils.build(dtoList);
    }

    @Override
    public List<BlogRouterDto> getAllBlogRouterList(Integer menuType) {
        List<BlogRouterEntity> routerList = baseDao.getAllRouterList(menuType);
        List<BlogRouterDto> dtoList = ConvertUtils.sourceToTarget(routerList, BlogRouterDto.class);
        return TreeUtils.build(dtoList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(BlogRouterDto dto) {
        BlogRouterEntity entity = ConvertUtils.sourceToTarget(dto, BlogRouterEntity.class);
        // 保存路由
        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BlogRouterDto dto) {
        BlogRouterEntity entity = ConvertUtils.sourceToTarget(dto, BlogRouterEntity.class);
        //上级菜单不能为自身
        if (entity.getId().equals(entity.getPid())) {
            throw new RenException(ErrorCode.SUPERIOR_MENU_ERROR, "上级菜单不能为自身");
        }

        //更新菜单
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        // 删除菜单
        deleteById(id);

        sysRoleRouterService.deleteByRouterId(id);

    }

    @Override
    public BlogRouterDto get(Long id) {
        BlogRouterEntity entity = baseDao.getById(id);
        BlogRouterDto dto = ConvertUtils.sourceToTarget(entity, BlogRouterDto.class);
        return dto;
    }

    @Override
    public List<BlogRouterDto> getListByPid(Long id) {
        List<BlogRouterEntity> list = baseDao.getListByPid(id);
        return ConvertUtils.sourceToTarget(list, BlogRouterDto.class);
    }


}
